package dao;

import domain.Product;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import utils.PageBean;
import utils.Utils;

import java.sql.SQLException;
import java.util.List;

/**
 * Created by guo on 2017/10/29.
 */
public class DaoImpl implements Dao{
    @Override
    public List<Product> findAll() {
        QueryRunner runner=new QueryRunner(Utils.getDs());
        String sql="select * from product";
        List<Product> list=null;
        try {
            list= (List<Product>) runner.query(sql,new BeanListHandler(Product.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public int deleteById(int pid) {
        QueryRunner runner=new QueryRunner(Utils.getDs());
        String sql="delete  from product where pid=?";
        int num=0;
        try {
            num= runner.update(sql,pid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return num;
    }

    @Override
    public List<Product> Search(String pnamme) {
        QueryRunner runner=new QueryRunner(Utils.getDs());
        String sql="select * from product where pname like ?";
        String newsql="%"+pnamme+"%";
        List<Product> list= null;
        try {
            list = (List<Product>) runner.query( sql, new BeanListHandler(Product.class),newsql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public int Add(Product product) {
        QueryRunner runner=new QueryRunner(Utils.getDs());
        String sql="insert into product(pname,market_price,pdesc)values(?,?,?)";
        int num=0;
        try {
            num=runner.update(sql,product.getPname(),product.getMarket_price(),product.getPdesc());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return num;
    }

    @Override
    public Product findById(int pid) {
        QueryRunner runner=new QueryRunner(Utils.getDs());
        String sql="select * from product where pid=?";
        Product product=null;
        try {
            product=(Product) runner.query(sql,new BeanHandler(Product.class),pid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return product;
    }

    @Override
    public int Update(Product product) {
        QueryRunner runner=new QueryRunner(Utils.getDs());
        String sql="update product set pname=?,market_price=?,pdesc=? where pid=?";
        int num=0;
        try {
            num=runner.update(sql,product.getPname(),product.getMarket_price(),product.getPdesc(),product.getPid());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return num;
    }

    @Override
    public PageBean findPage(int currentPage, int pageSize) {
          /*创建一个对象*/
        PageBean pageBean=new PageBean();

        //将数据都放到pageBean里
        pageBean.setCurrenPage(currentPage);
        pageBean.setPageSize(pageSize);

        QueryRunner runner=new QueryRunner(Utils.getDs());
        //两条查询语句
        /*分页查询*/
        String sql="select * from product order by pid limit ?,?";
        /*查询总数*/
        String countSql="select count(*) from product";
        try {
            List<Product> list= (List<Product>) runner.query(sql,new BeanListHandler(Product.class),(currentPage-1)*pageSize,pageSize);
            pageBean.setPageList(list);

            Long c= (Long) runner.query(countSql,new ScalarHandler());
            //将long型转换为int类型
            int count=c.intValue();
            pageBean.setCount(count);


        } catch (SQLException e) {
            e.printStackTrace();
        }
        return pageBean;
    }

}

